package com.briup.homework.chap11;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test029_ObjectStream {
    private final static String FILE_PATH = "D:/test/obj_file";
    public static void save() {
        // 补全代码
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        List<Person> list = new ArrayList<Person>();
        Person p1 = new Person("张三","男");
        Person p2 = new Person("李四","女");
        Person p3 = new Person("王五","男");
        Collections.addAll(list,p1,p2,p3);
        try {
            fos = new FileOutputStream(FILE_PATH);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    public static void load() {
        // 补全代码
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        List<Person> list = new ArrayList<Person>();
        try {
            fis = new FileInputStream(FILE_PATH);
            ois = new ObjectInputStream(fis);
            Object o = ois.readObject();
            if(o instanceof List<?> l){
                for(Object obj:l){
                    if(obj instanceof Person p){
                        list.add(p);
                    }
                }
            }
            // 遍历一下
            list.forEach(System.out::println);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

    public static void main(String[] args) {
        save();
        load();
    }
}
class Person implements Serializable {
    private static final long serialVersionUID = 4258798319750620830L;
    private String name;
    private String gender;
    public Person() {
    }
    public Person(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", gender=" + gender + "]";
    }
}